// pages/cart/cart-page.js
import { RequestManager } from '../../util/requestManager.js'

const app = getApp()
let requestManager = new RequestManager()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    conentMarginTop: 0,
    cartItems: [],
    allSelected: false,
    totalAmount:'0.00'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      conentMarginTop: app.globalData.navHeight
    })
  },

  onShow: function(){
    this.private_downloadData()
  },

  //  分享给好友
  onShareAppMessage: function () {
    return app.public_getShareInfo()
  },


  //  从网络接口中获取购物车的数据
  private_downloadData: function () {
    var that = this;
    requestManager.getAllCartList(app.globalData.openid,result=>{
      this.setData({
        cartItems: result,
        allSelected: false,
        totalAmount: '0.00'
      })
      //  赋值给全局变量
      app.globalCart = result
    });
  },
  //  全选商品
  action_check: function (event) {
    let is_selected = event.detail.checked;
    var tmpCartItems = this.data.cartItems;
    tmpCartItems.forEach((item) => {
      item.is_selected = is_selected;
    });
    this.setData({
      cartItems: tmpCartItems
    })
    app.globalCart = tmpCartItems;
    this.private_computeCartData();
  },
  //  监听商品数量
  observer_item_count:function(event){
    let observerItem = event.detail;
    var tmpCartItems = this.data.cartItems;
    var spliceIndex = 0;
    tmpCartItems.forEach((item,index) => {
      if (item.id === observerItem.id) {
        item.count = event.detail.count;
        spliceIndex = index;
      }
    });
    
    if (observerItem.count == 0){
      //  当操作数据对象当count值为0时，需要从数组中把对象删除并且调用删除接口
      tmpCartItems.splice(spliceIndex,1);
      requestManager.removeGoodsItemFromCart(app.globalData.openid,observerItem.id,success=>{
      })
    }
    else {
      //  调用网络请求接口，更新购物车中商品的数量
      requestManager.updateGoodsItemToCart(app.globalData.openid,observerItem.id,observerItem.count,resulut=>{

      })
    }

    this.setData({
      cartItems: tmpCartItems
    })
    this.private_computeCartData();
  },
  //  监听商品是否全选
  observer_item_selected: function (event) {

    var tmpAllSelected = true;
    var tmpCartItems = this.data.cartItems;

    tmpCartItems.forEach((item) => {
      
      if(item.id === event.detail.id){
        item.is_selected = event.detail.is_selected;
      }
      tmpAllSelected = tmpAllSelected && item.is_selected;
    });
    this.setData({
      cartItems: tmpCartItems,
      allSelected: tmpAllSelected
    })
    this.private_computeCartData();
  },
  //  统计购物车里的数据
  private_computeCartData: function () {
    var tmpCartItems = this.data.cartItems;
    var totalAmount = 0.00;
    tmpCartItems.forEach((tmpItem) => {
      if(tmpItem.is_selected){
        totalAmount += parseFloat(tmpItem.price * tmpItem.count);
      }
    })
    this.setData({
      totalAmount: totalAmount.toFixed(2),
    });
  },
  //提交数据  
  action_submit: function () {

    var tmpCartItems = this.data.cartItems;
    var selectedItems = []

    var isSellOut = false

    tmpCartItems.forEach((tmpItem) => {
      if (tmpItem.is_selected) {
        selectedItems.push(tmpItem);
        tmpItem.showAmountText();
        isSellOut = isSellOut || (tmpItem.goodsItem.invertory == 0 ? true : false);
      }
    })

    if(isSellOut) {
      wx.showToast({
        title: "已选择了售罄的产品，请重新选择；也可联系客服进行咨询",
        icon: 'none'
      });
      return;
    }

    if(selectedItems.length == 0){
      wx.showToast({
        title: "请先选择需要购买的商品",
        icon: 'none'
      });
      return;
    }
    wx.navigateTo({
      url: '/pages/order/order-page?items=' + JSON.stringify(selectedItems),
    })
  },
})